MAZE ALGORITHM ============== Every room has a number Start in Room 1 Exit in Room N (max room number) ITERATIVE SOLUTION ================== CurrentRoom = 1 VisitedRooms = new Set() // all rooms that we've visited VisitedRoom.add(1) Path = new Stack() // path followed to reach CurrentRoom While (CurrentRoom != N) NextRoom = Any room reachable from CurrentRoom that's not in VisitedRooms If NextRoom >= 1 // Move forward Path.push(CurrentRoom) VisitedRooms.add(NextRoom) CurrentRoom = NextRoom Else // Backtrack CurrentRoom = Path.pop() Path.push(CurrentRoom) // same as Path.push(N) Solution = new Stack(); while (!Path.isEmpty()) Solution.push(Path.pop()) RECURSIVE SOLUTION ================== VisitedRooms = new Set() // all rooms that we've visited Solution = new Stack() SolveMaze(1) Boolean SolveMaze(int CurrentRoom) VisitedRooms.add(CurrentRoom) If (CurrentRoom == N) Solution.push(CurrentRoom) return True Else NextRoom = Any room reachable from CurrentRoom that's not in VisitedRooms While (NextRoom >= 1) if (SolveMaze(NextRoom)) Solution.push(CurrentRoom) return True NextRoom = Any room reachable from CurrentRoom that's not in VisitedRooms return False